//---------------------------------------------------------------------------
#include "CLocation.h"
#include <Dialogs.hpp>
//---------------------------------------------------------------------------

__fastcall CLocation::CLocation( void )
{
    m_Time = TDateTime::CurrentDateTime();
}

//---------------------------------------------------------------------------

void __fastcall CLocation::Ajouter( void )
{
    try {
        bool bRetour = true;
        TIBQuery * MaRequete = NULL;
        MaRequete = new TIBQuery((TComponent*)0);
        MaRequete->Database = dmKiloutou->dbKiloutou;

        MaRequete->SQL->Text =  "select  CLIENT.NOM,"
                                        "CLIENT.PRENOM,"
                                        "MATERIEL.NOM,"
                                        "LOCATION.DEBUT_LOCATION, LOCATION.RETOUR_LOCATION "
                                "from    MATERIEL, CLIENT, LOCATION "
                                "WHERE   LOCATION.CODE_CLIENT = CLIENT.CODE_CLIENT "
                                "AND "
                                        "LOCATION.CODE_MATERIEL= MATERIEL.CODE_MATERIEL";
        MaRequete->Open();
        MaRequete->First();


        while(!MaRequete->Eof)
        {
            if(     (asMateriel(NOM) == MaRequete->Fields->Fields[2]->AsString )
                &&  (asClient(NOM) == MaRequete->Fields->Fields[0]->AsString) )
            {
                if ( MaRequete->Fields->Fields[4]->AsDateTime == TDateTime(0) ) {
                    //Si Matériel en cours de location avec la meme personne, déja saisie
                    MessageDlg( "Déjà saisie", mtWarning, TMsgDlgButtons() << mbOK, 0);
                    bRetour = false;

                } else {
                    //Si Matériel a déjà été loué par ce client, client fidele au materiel
                    MessageDlg( (AnsiString)"Client fidèle à " + asMateriel(NOM),
                                mtWarning,
                                TMsgDlgButtons() << mbOK, 0);
                }
                break;
            }

            //Si matériel en cours de location, Impossible matériel déjà loué
            if(    (asMateriel(NOM) == MaRequete->Fields->Fields[2]->AsString )
                && (MaRequete->Fields->Fields[4]->AsDateTime == TDateTime(0)) )
            {
                //affichage de la fenetre d'avertissement
                MessageDlg( (AnsiString)"Impossible matériel "+ asMateriel(NOM) +" déjà loué",
                            mtWarning,
                            TMsgDlgButtons() << mbOK, 0);
                bRetour = false;
                break;
            }
            MaRequete->Next();
        }

        MaRequete->SQL->Clear();
        MaRequete->Close();

        if(bRetour) {
          dmKiloutou->tLocation->Insert();
          nLocation(CODE_CLIENT) = nClient(CODE_CLIENT);
          nLocation(CODE_MATERIEL) = nMateriel(CODE_MATERIEL);
          dtLocation(DEBUT_LOCATION) = m_Time.FormatString("dd/mm/yyyy hh:nn:ss");
          dmKiloutou->tLocation->Post();
          dmKiloutou->trKiloutou->CommitRetaining();

          //Refresh du dataset
          dmKiloutou->dsetLocation->Active = false;
          dmKiloutou->dsetLocation->Active = true;
        }
        if(MaRequete)
        {
            delete MaRequete;
            MaRequete = NULL;
        }

    } catch (Exception &e) {
        EIC_DISPLAY_EXCEPTION(e);
    }
}

void __fastcall CLocation::Rendre( void )
{
    if(dmKiloutou->dsetLocationRETOUR_LOCATION->AsDateTime == TDateTime(0)) {
      TIBQuery * MaRequete = NULL;
      MaRequete = new TIBQuery((TComponent*)0);
      MaRequete->Database = dmKiloutou->dbKiloutou;

      MaRequete->SQL->Text = "update location set location.RETOUR_LOCATION = \'"+m_Time.FormatString("mm/dd/yyyy hh:nn:ss")+
                              "\' where CODE_LOCATION = "+dmKiloutou->dsetLocationCODE_LOCATION->AsInteger;
      MaRequete->ExecSQL();
      MaRequete->SQL->Clear();

      MaRequete->SQL->Text = "Commit";
      MaRequete->ExecSQL();
      MaRequete->SQL->Clear();
      MaRequete->Close();

      if(MaRequete) {delete MaRequete; MaRequete = NULL;}

      //Refresh du dataset
      dmKiloutou->dsetLocation->Active = false;
      dmKiloutou->dsetLocation->Active = true;
    }
    else {
        MessageDlg("Matériel déjà rendu", mtWarning, TMsgDlgButtons() << mbOK, 0);
    }
}

void __fastcall CLocation::EffacerTable( void )
{
    dmKiloutou->dsLocation->DataSet->Delete();
    dmKiloutou->trKiloutou->CommitRetaining();

        //Refresh du dataset
    dmKiloutou->dsetLocation->Active = false;
    dmKiloutou->dsetLocation->Active = true;
}
